package com.hmall.controller.back;

import com.hdmall.business.common.common.ServerResponse;

import com.hmall.mapper.MallNavigationItemMapper;
import com.hmall.mapper.MallNavigationLogMapper;
import com.hmall.mapper.MallNavigationMapper;
import com.hmall.mapper.SysUserMapper;

import com.hmall.model.MallNavigation;
import com.hmall.model.MallNavigationItem;
import com.hmall.model.MallNavigationLog;
import com.hmall.model.SysUser;

import com.hmall.util.TokenUtil;

import com.hmall.vo.MallNavigationVo;

import com.hmall.vo.PageVo;
import com.hmall.vo.ShopProductListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/manage/navigation")
@Api(value = "导航管理操作API", description = "导航管理类API说明")
public class NavigationMangeController {
    private final
    SysUserMapper sysUserMapper;
    private final
    MallNavigationMapper mallNavigationMapper;
    private final
    MallNavigationItemMapper mallNavigationItemMapper;
    private final
    MallNavigationLogMapper mallNavigationLogMapper;
    private final
    JedisPool jedisPool;

    /*@Autowired
    RedisOperator redisOperator=new RedisOperator();*/
    private TokenUtil tokenUtil = new TokenUtil();

    @Autowired
    public NavigationMangeController(SysUserMapper sysUserMapper, MallNavigationMapper mallNavigationMapper, MallNavigationItemMapper mallNavigationItemMapper, MallNavigationLogMapper mallNavigationLogMapper, JedisPool jedisPool) {
        this.sysUserMapper = sysUserMapper;
        this.mallNavigationMapper = mallNavigationMapper;
        this.mallNavigationItemMapper = mallNavigationItemMapper;
        this.mallNavigationLogMapper = mallNavigationLogMapper;
        this.jedisPool = jedisPool;
    }

    @PostMapping("addNavigation")
    @ApiOperation(value = "添加导航")
    public ServerResponse addNavigation(String title, String token, Integer sort) throws Exception {
        SysUser user;
        Jedis jedis = jedisPool.getResource();
        user = tokenUtil.tokenToUser(jedis, token).getData();
        jedis.close();

        if (user == null) {
            return ServerResponse.createByErrorMessage("token不存在或失效");
        }

        Integer count = mallNavigationMapper.selectByTitle(title);

        if (count != 0) {
            return ServerResponse.createByErrorMessage("导航标题已存在");
        }

        MallNavigation mallNavigation = new MallNavigation();
        mallNavigation.setTitle(title);
        mallNavigation.setCreateTime(new Date());
        mallNavigation.setSort(sort);
        mallNavigationMapper.insert(mallNavigation);


        MallNavigationLog mallNavigationLog = new MallNavigationLog();
        mallNavigationLog.setCreateTime(new Date());
        mallNavigationLog.setParams(title);
        mallNavigationLog.setType("添加导航");
        mallNavigationLog.setUserId(user.getUserId());
        mallNavigationLog.setSort(sort);
        mallNavigationLogMapper.insert(mallNavigationLog);
        return ServerResponse.createBySuccess("添加成功");
    }

    @PostMapping("editNavigation")
    @ApiOperation(value = "修改导航")
    public ServerResponse editNavigation(Integer navigation_id, String title, String token, Integer sort) throws Exception {
        SysUser user;
        Jedis jedis = jedisPool.getResource();
        user = tokenUtil.tokenToUser(jedis, token).getData();
        jedis.close();

        if (user == null) {
            return ServerResponse.createByErrorMessage("token不存在或失效");
        }

        MallNavigation mallNavigation = mallNavigationMapper.selectByPrimaryKey(navigation_id);
        mallNavigation.setUpdateTime(new Date());
        mallNavigation.setTitle(title);
        mallNavigation.setSort(sort);
        mallNavigationMapper.updateByPrimaryKey(mallNavigation);

        MallNavigationLog mallNavigationLog = new MallNavigationLog();
        mallNavigationLog.setCreateTime(new Date());
        mallNavigationLog.setParams(title);
        mallNavigationLog.setType("修改导航");
        mallNavigationLog.setUserId(user.getUserId());
        mallNavigationLog.setSort(sort);
        mallNavigationLogMapper.insert(mallNavigationLog);

        return ServerResponse.createBySuccess("修改成功");
    }

    @PostMapping("deleteNavigation")
    @ApiOperation(value = "删除导航")
    public ServerResponse deleteNavigation(Integer navigation_id, String token) throws Exception {
        SysUser user;
        Jedis jedis = jedisPool.getResource();
        user = tokenUtil.tokenToUser(jedis, token).getData();
        jedis.close();

        if (user == null) {
            return ServerResponse.createByErrorMessage("token不存在或失效");
        }

        MallNavigation mallNavigation = mallNavigationMapper.selectByPrimaryKey(navigation_id);

        MallNavigationLog mallNavigationLog = new MallNavigationLog();
        mallNavigationLog.setCreateTime(new Date());
        mallNavigationLog.setParams(mallNavigation.getTitle());
        mallNavigationLog.setType("删除导航");
        mallNavigationLog.setUserId(user.getUserId());
        mallNavigationLog.setSort(mallNavigation.getSort());
        mallNavigationLogMapper.insert(mallNavigationLog);

        List<MallNavigationItem> list = mallNavigationItemMapper.selectByNavigationId(navigation_id);
        for (MallNavigationItem aList : list) {
            MallNavigationLog mallNavigationLog2 = new MallNavigationLog();
            mallNavigationLog2.setCreateTime(new Date());
            mallNavigationLog2.setTitle(mallNavigation.getTitle());
            mallNavigationLog2.setParams(aList.getName());
            mallNavigationLog2.setType("删除导航子项");
            mallNavigationLog2.setLink(aList.getLink());
            mallNavigationLog2.setNavDesc(aList.getNavDesc());
            mallNavigationLog2.setUserId(user.getUserId());
            mallNavigationLogMapper.insert(mallNavigationLog2);
        }

        mallNavigationMapper.deleteByPrimaryKey(navigation_id);
        mallNavigationItemMapper.deleteByNavId(navigation_id);

        return ServerResponse.createBySuccess("删除成功");
    }

    @PostMapping("addNavigationItem")
    @ApiOperation(value = "添加导航子项")
    public ServerResponse addNavigationItem(String icon, String name,
                                            String desc, String link, Integer navigation_id,
                                            String token) throws Exception {
        SysUser user;
        Jedis jedis = jedisPool.getResource();
        user = tokenUtil.tokenToUser(jedis, token).getData();
        jedis.close();

        if (user == null) {
            return ServerResponse.createByErrorMessage("token不存在或失效");
        }

        Integer count = mallNavigationItemMapper.selectByNameAndId(name,
                navigation_id);

        if (count != 0) {
            return ServerResponse.createByErrorMessage("导航子项已存在");
        }

        MallNavigationItem mallNavigationItem = new MallNavigationItem();
        mallNavigationItem.setNavDesc(desc);
        mallNavigationItem.setIcon(icon);
        mallNavigationItem.setLink(link);
        mallNavigationItem.setName(name);
        mallNavigationItem.setNavigationId(navigation_id);
        mallNavigationItem.setCreateTime(new Date());
        mallNavigationItem.setAuthor(user.getUsername());
        mallNavigationItemMapper.insert(mallNavigationItem);

        MallNavigationLog mallNavigationLog = new MallNavigationLog();
        mallNavigationLog.setCreateTime(new Date());
        mallNavigationLog.setParams(name);
        mallNavigationLog.setType("添加导航子项");
        mallNavigationLog.setLink(link);
        mallNavigationLog.setNavDesc(desc);
        mallNavigationLog.setUserId(user.getUserId());
        MallNavigation mallNavigation = mallNavigationMapper.selectByPrimaryKey(navigation_id);
        mallNavigationLog.setTitle(mallNavigation.getTitle());
        mallNavigationLogMapper.insert(mallNavigationLog);

        return ServerResponse.createBySuccess("添加成功");
    }

    @PostMapping("editNavigationItem")
    @ApiOperation(value = "修改导航子项")
    public ServerResponse editNavigationItem(Integer navigation_id, Integer nav_id, String icon, String name,
                                             String desc, String link, String token) throws Exception {
        SysUser user;
        Jedis jedis = jedisPool.getResource();
        user = tokenUtil.tokenToUser(jedis, token).getData();
        jedis.close();

        if (user == null) {
            return ServerResponse.createByErrorMessage("token不存在或失效");
        }

        Integer count = mallNavigationItemMapper.selectByNameAndNavId(name,
                navigation_id, nav_id);

        if (count > 1) {
            return ServerResponse.createByErrorMessage("导航子项已存在");
        }
        MallNavigationItem mallNavigationItem = mallNavigationItemMapper.selectByPrimaryKey(nav_id);
        mallNavigationItem.setNavDesc(desc);
        mallNavigationItem.setName(name);
        mallNavigationItem.setLink(link);
        mallNavigationItem.setIcon(icon);
        mallNavigationItem.setUpdateTime(new Date());
        mallNavigationItem.setNavigationId(navigation_id);
        mallNavigationItem.setAuthor(user.getUsername());
        mallNavigationItemMapper.updateByPrimaryKey(mallNavigationItem);

        MallNavigationLog mallNavigationLog = new MallNavigationLog();
        mallNavigationLog.setCreateTime(new Date());
        mallNavigationLog.setParams(name);
        mallNavigationLog.setType("修改导航子项");
        mallNavigationLog.setLink(link);
        mallNavigationLog.setNavDesc(desc);
        mallNavigationLog.setUserId(user.getUserId());
        MallNavigation mallNavigation = mallNavigationMapper.selectByPrimaryKey(navigation_id);
        mallNavigationLog.setTitle(mallNavigation.getTitle());
        mallNavigationLogMapper.insert(mallNavigationLog);

        return ServerResponse.createBySuccess("修改成功");
    }

    @PostMapping("deleteNavigationItem")
    @ApiOperation(value = "删除导航子项")
    public ServerResponse deleteNavigationItem(Integer nav_id, String token) throws Exception {
        SysUser user;
        Jedis jedis = jedisPool.getResource();
        user = tokenUtil.tokenToUser(jedis, token).getData();
        jedis.close();

        if (user == null) {
            return ServerResponse.createByErrorMessage("token不存在或失效");
        }
        MallNavigationItem mallNavigationItem = mallNavigationItemMapper.selectByPrimaryKey(nav_id);
        MallNavigationLog mallNavigationLog = new MallNavigationLog();
        MallNavigation mallNavigation = mallNavigationMapper.selectByPrimaryKey(mallNavigationItem.getNavigationId());
        mallNavigationLog.setTitle(mallNavigation.getTitle());
        mallNavigationLog.setCreateTime(new Date());
        mallNavigationLog.setParams(mallNavigationItem.getName());
        mallNavigationLog.setType("删除导航子项");
        mallNavigationLog.setLink(mallNavigationLog.getLink());
        mallNavigationLog.setNavDesc(mallNavigationLog.getNavDesc());
        mallNavigationLog.setUserId(user.getUserId());
        mallNavigationLogMapper.insert(mallNavigationLog);

        mallNavigationItemMapper.deleteByPrimaryKey(nav_id);

        return ServerResponse.createBySuccess("删除成功");
    }

    @GetMapping("getNavigationList")
    @ApiOperation(value = "获得导航标题列表")
    public ServerResponse<ArrayList<MallNavigationVo>> getTitleList() {
        List<MallNavigation> mallNavigationList = mallNavigationMapper.selectAllNavigation();
        ArrayList<MallNavigationVo> mallNavigationVoList = new ArrayList<>();

        for (MallNavigation aMallNavigationList : mallNavigationList) {
            List<MallNavigationItem> mallNavigationItemList = mallNavigationItemMapper.selectByNavigationId(aMallNavigationList.getNavigationId());
            MallNavigationVo mallNavigationVo = new MallNavigationVo();
            mallNavigationVo.setNavigationItemList(mallNavigationItemList);
            mallNavigationVo.setTitle(aMallNavigationList.getTitle());
            mallNavigationVo.setNavigation_id(aMallNavigationList
                    .getNavigationId());
            mallNavigationVo.setSort(aMallNavigationList.getSort());
            mallNavigationVoList.add(mallNavigationVo);
        }

        return ServerResponse.createBySuccess(mallNavigationVoList);
    }

    @GetMapping("getChildNavigationTitleList")
    @ApiOperation(value = "获得导航子项标题列表")
    public ServerResponse getChildNavigationTitleList(@RequestParam(value = "pageNum", defaultValue = "1")
                                                              int pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "100")
                                                              int pageSize) {
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        total = mallNavigationItemMapper.selectAll().size();
        List<MallNavigationItem> list = mallNavigationItemMapper.selectAllWithPage(pageStart, pageSize);
        PageVo pageVo = new PageVo<>(pageNum, pageSize, total, list);
        return ServerResponse.createBySuccess(pageVo);
    }

    @GetMapping("checkRole")
    @ApiOperation(value = "判断权限")
    public ServerResponse checkRole(String token) throws Exception {
        SysUser user;
        Jedis jedis = jedisPool.getResource();
        user = tokenUtil.tokenToUser(jedis, token).getData();
        jedis.close();

        if (user == null) {
            return ServerResponse.createByErrorMessage("token不存在或失效");
        }

        SysUser sysUser = sysUserMapper.selectByPrimaryKey(user.getUserId());

        if (sysUser.getDeptId() == null) {
            return ServerResponse.createBySuccess(0);
        }

        return ServerResponse.createBySuccess(1);
    }

    @GetMapping("getNavigationLog")
    @ApiOperation(value = "获得导航日志")
    public ServerResponse<List<MallNavigationLog>> getNavigationLog() {
        List<MallNavigationLog> list = mallNavigationLogMapper.selectAllLog();
        return ServerResponse.createBySuccess(list);
    }

    @PostMapping("checkSort")
    @ApiOperation(value = "校验排序是否存在")
    public ServerResponse checkSort(Integer sort) {
        Integer count = mallNavigationMapper.checkSort(sort);
        if (count != 0) {
            return ServerResponse.createByErrorMessage("排序已存在");
        }
        return ServerResponse.createBySuccess("排序不存在");
    }

    @PostMapping("changeSort")
    @ApiOperation(value = "交换排序")
    public ServerResponse<String> changeSort(Integer navigation_id1, Integer navigation_id2) {
        MallNavigation mallNavigation1 = mallNavigationMapper.selectByPrimaryKey(navigation_id1);
        MallNavigation mallNavigation2 = mallNavigationMapper.selectByPrimaryKey(navigation_id2);
        Integer temp = mallNavigation2.getSort();
        mallNavigation2.setSort(mallNavigation1.getSort());
        mallNavigation1.setSort(temp);
        mallNavigationMapper.updateByPrimaryKey(mallNavigation1);
        mallNavigationMapper.updateByPrimaryKey(mallNavigation2);
        return ServerResponse.createBySuccess("交换成功");
    }
}
